<template>
  <Form
    ref="target"
    class="xtx-form"
    :validation-schema="rules"
    v-slot="{ errors }"
  >
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{ name }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field
          name="mobile"
          v-model="form.mobile"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="error" v-if="errors.mobile">{{ errors.mobile }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field
          name="code"
          v-model="form.code"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span v-if="totalTime === 6" class="code" @click="getCode"
          >发送验证码</span
        >
        <span v-else class="code">{{ totalTime }}秒后重试</span>
      </div>
      <div class="error" v-if="errors.code">{{ errors.code }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即绑定</a>
  </Form>
</template>

<script>
import qc from 'qc'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ref, reactive } from 'vue'
import { Form, Field } from 'vee-validate'
import validateObj from '@/utils/validate'
import { useIntervalFn } from '@vueuse/core'
import { loginSocialCode, loginSocialBind } from '@/api/user'
import { Message } from '@/components/library/Message'
export default {
  name: 'CallbackBind',
  components: {
    Form,
    Field
  },
  props: ['unionId'],
  setup (props) {
    /*
    qq和已有小兔鲜帐号的绑定
    1：通过qc获取当前qq用户的头像与昵称
    2：表单验证
    3：发送验证码
    4：调用绑定接口处理绑定
   */
    const name = ref('')
    const avatar = ref('')
    const target = ref(null)
    const store = useStore()
    const router = useRouter()
    const form = reactive({
      mobile: '136',
      code: ''
    })
    const rules = {
      mobile: validateObj.mobile,
      code: validateObj.code
    }
    if (qc.Login.check()) {
      qc.api('get_user_info').success(res => {
        name.value = res.data.nickname
        avatar.value = res.data.figureurl_1
        console.log('用户信息', res)
      })
    }
    // 确定按钮点击
    function submit () {
      target.value.validate().then(res => {
        if (res) {
          console.log('表单验证结果:', res)
          // 定义绑定api,导入调用,调用成功返回进入的页面
          loginSocialBind({
            unionId: props.unionId,
            ...form
          }).then(res => {
            // 存储用户信息与token到vuex
            store.commit('user/setUserInfo', res.result)
            Message({ type: 'success', text: '绑定成功' })
            console.log(store.state.user.redirect)
            router.push(store.state.user.redirect)
            // console.log('绑定后返回信息', res)
          })
        }
      })
    }
    // 验证码处理
    const totalTime = ref(6)
    const { pause, resume } = useIntervalFn(
      () => {
        totalTime.value--
        if (totalTime.value <= 0) {
          pause()
          totalTime.value = 6
        }
      },
      1000,
      {
        immediate: false,
        immediateCallback: true
      }
    )

    function getCode () {
      // 1:局部表单验证
      const _result = validateObj.mobile(form.mobile)
      if (_result === true) {
        // 2：倒计时处理
        resume()
        // 3：调用接口获取验证码
        loginSocialCode(form.mobile).then(res => {
          console.log('验证码信息', res)
        })
      } else {
        target.value.setFieldError('mobile', _result)
      }
    }

    return { name, avatar, form, rules, submit, target, getCode, totalTime }
  }
}
</script>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
// 表单
.xtx-form {
  padding: 50px 0;
  &-item {
    display: flex;
    align-items: center;
    width: 700px;
    margin: 0 auto;
    padding-bottom: 25px;
    .label {
      width: 180px;
      padding-right: 10px;
      text-align: right;
      color: #999;
      ~ .field {
        margin-left: 0;
      }
    }
    .field {
      width: 320px;
      height: 50px;
      position: relative;
      margin-left: 190px;
      .icon {
        position: absolute;
        left: 0;
        top: 0;
        width: 40px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        color: #999;
        ~ .input {
          padding-left: 40px;
        }
      }
      .input {
        border: 1px solid #e4e4e4;
        width: 320px;
        height: 50px;
        line-height: 50px;
        padding: 0 10px;
        &.err {
          border-color: @priceColor;
        }
        &:focus,
        &:active {
          border-color: @xtxColor;
        }
      }
    }
    .error {
      width: 180px;
      padding-left: 10px;
      color: @priceColor;
    }
  }
  .submit {
    width: 320px;
    height: 50px;
    border-radius: 4px;
    background: @xtxColor;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-size: 16px;
    color: #fff;
    display: block;
    margin: 0 auto;
  }
}
</style>
